<template>
  <div>
    <!-- 面包屑导航区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">
        当前位置：首页
      </el-breadcrumb-item>
      <el-breadcrumb-item>医院管理</el-breadcrumb-item>
      <el-breadcrumb-item>医院管理</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片视图区域 -->
    <el-card>
      <!-- 搜索区 -->
      <el-form
        ref="searchFormRef"
        :inline="true"
        :model="queryInfo.query"
        size="mini"
      >
        <el-form-item label="医院名称【ID】" prop="hospital_id">
          <el-select
            v-model="queryInfo.query.hospital_id"
            placeholder="请选择"
            filterable
            clearable
          >
            <el-option
              v-for="item in selectData.get_hospital"
              :key="item.id"
              :label="item.title + '【' + item.id + '】'"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="医院类型" prop="is_self">
          <el-select
            v-model="queryInfo.query.is_self"
            placeholder="请选择"
            filterable
            clearable
          >
            <el-option key="1" label="自营" value="1"></el-option>
            <el-option key="0" label="第三方" value="0"></el-option>
          </el-select>
        </el-form-item>
        <!-- 归属服务商 -->
        <el-form-item label="归属服务商【ID】" prop="provider_id">
          <el-select v-model="queryInfo.query.provider_id" filterable clearable>
            <el-option
              v-for="item in selectData.provider"
              :key="item.id"
              :label="item.name + '【' + item.id + '】'"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <!-- 状态 -->
        <el-form-item label="状态" prop="status">
          <el-select v-model="queryInfo.query.status" filterable clearable>
            <el-option key="1" label="待审核" value="1"></el-option>
            <el-option key="2" label="开启" value="2"></el-option>
            <el-option key="3" label="关闭" value="3"></el-option>
            <el-option key="4" label="审核拒绝" value="4"></el-option>
          </el-select>
        </el-form-item>
        <!-- 所在区域 -->
        <el-form-item label="所在区域" prop="area">
          <el-cascader
            :options="options"
            :props="props"
            clearable
            filterable
            v-model="queryInfo.query.area"
          ></el-cascader>
        </el-form-item>
        <el-form-item>
          <el-button
            class="theme-background-color"
            size="mini"
            @click="searchList"
          >
            搜索
          </el-button>
          <el-button size="mini" @click="resetSearchForm"> 重置 </el-button>
        </el-form-item>
      </el-form>

      <!-- 操作区域 -->
      <div>
        <el-button
          size="mini"
          :loading="btnLoading"
          class="theme-background-color"
          icon="el-icon-plus"
          @click="$router.push('/hospital/AddHospital')"
        >
          新增医院
        </el-button>
        <!-- 导出数据 -->
        <el-button
          icon="el-icon-download"
          size="mini"
          :loading="btnLoading"
          @click="downloadExcel"
          >导出数据</el-button
        >
      </div>

      <!-- 列表区域 -->
      <el-table :data="list" border stripe>
        <el-table-column label="医院名称【ID】" min-width="260px">
          <template slot-scope="scope">
            <el-link type="primary" @click="toDetail(scope.row.id)">
              {{ scope.row.title }}【{{ scope.row.id }}】
            </el-link>
          </template>
        </el-table-column>
        <el-table-column label="医院类型" min-width="100px">
          <template slot-scope="scope">
            <span v-if="scope.row.is_self == 0">第三方</span>
            <span v-else>自营</span>
          </template>
        </el-table-column>
        <el-table-column label="归属服务商【ID】" min-width="260px">
          <template slot-scope="scope">
            <span
              v-show="
                scope.row.provider_id != null && scope.row.provider_id != ''
              "
            >
              {{ scope.row.providerName }}【{{ scope.row.provider_id }}】
            </span>
          </template>
        </el-table-column>
        <el-table-column
          label="医院电话"
          min-width="120px"
          prop="tel"
        ></el-table-column>
        <el-table-column label="所在区域" min-width="220px">
          <template slot-scope="scope">
            {{ scope.row.province }} {{ scope.row.city }} {{ scope.row.area }}
          </template>
        </el-table-column>
        <el-table-column
          label="详细地址"
          min-width="180px"
          prop="address"
        ></el-table-column>
        <el-table-column
          label="营业项目"
          min-width="180px"
          prop="serviceItemName"
        ></el-table-column>
        <el-table-column
          label="医院负责人"
          min-width="100px"
          prop="leading_name"
        ></el-table-column>
        <el-table-column
          label="负责人电话"
          min-width="120px"
          prop="leading_phone"
        ></el-table-column>
        <el-table-column
          label="状态"
          min-width="100px"
          prop="statusName"
        ></el-table-column>
        <el-table-column label="操作" min-width="100px" fixed="right">
          <template slot-scope="scope">
            <el-link
              type="primary"
              @click="
                $router.push({
                  path: '/hospital/Staff',
                  query: { id: scope.row.id }
                })
              "
              v-if="
                scope.row.audit_status == 1 &&
                scope.row.status == 1 &&
                scope.row.use_system == 1
              "
            >
              员工管理
            </el-link>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页区域 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.page"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="queryInfo.size"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
import regionData from '../../assets/js/regionChoose.js'
export default {
  data() {
    return {
      queryInfo: {
        query: {
          hospital_id: '',
          is_self: '',
          provider_id: '',
          status: '',
          area: []
        },
        page: 1,
        size: 20
      },
      selectData: [],
      list: [],
      total: 0,
      btnLoading: false,

      options: regionData,
      props: {
        checkStrictly: true,
        value: 'name',
        label: 'name',
        children: 'childs'
      }
    }
  },
  created() {
    this.getListData()
    this.getSelectData()
  },
  methods: {
    async getSelectData() {
      const url = 'admin/Data/find'
      const { data: res } = await this.$http.post(url, {
        code: 'get_doctor,get_hospital,provider'
      })
      if (res.meta.status !== 200) {
        return this.$message.error(res.meta.msg)
      }
      this.selectData = res.data
    },
    searchList() {
      this.queryInfo.page = 1
      this.getListData()
    },
    resetSearchForm() {
      this.$refs.searchFormRef.resetFields()
      this.getListData()
    },
    // 表格数据
    async getListData() {
      // 返回数据重命名为res
      const { data: res } = await this.$http.post(
        '/admin/Hospital/pageList',
        this.queryInfo
      )
      if (res.meta.status !== 200) {
        return this.$message.error(res.meta.msg)
      }
      this.list = res.data.list
      this.total = res.data.total
    },
    // 监听 pagesize 改变的事件
    handleSizeChange(newSize) {
      console.log(newSize)
      this.queryInfo.size = newSize
      this.getListData()
    },
    // 监听 页码值 改变的事件
    handleCurrentChange(newPage) {
      console.log(newPage)
      this.queryInfo.page = newPage
      this.getListData()
    },
    toDetail(id) {
      const newpage = this.$router.resolve({
        path: '/hospital/HospitalDetail',
        query: { id: id }
      })
      window.open(newpage.href, '_blank')
    },
    async downloadExcel() {
      this.btnLoading = true
      this.$http({
        method: 'post',
        url: '/admin/Hospital/downloadExcel',
        data: this.queryInfo.query,
        headers: {
          token: window.sessionStorage.getItem('token')
        },
        responseType: 'blob'
      })
        .then((res) => {
          this.btnLoading = false
          if (res.data.type) {
            // 文件下载
            const blob = new Blob([res.data], {
              type: 'application/vnd.ms-excel'
            })
            let link = document.createElement('a')
            link.href = URL.createObjectURL(blob)
            link.setAttribute('download', '医院数据.xlsx')
            link.click()
            link = null
            this.$message.success('导出成功')
          } else {
            // 返回json
            this.$message.warning(res.data.msg)
          }
        })
        .catch((err) => {
          this.btnLoading = false
          console.log(err)
          this.$message.error('下载失败')
        })
    }
  }
}
</script>

<style lang="less" scoped>
</style>
